library(RSQLite)
library(ggplot2)
library(maps)
library(usdata)
library(dplyr)
##
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
##
## filter, lag
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
library(ggmap)
## Google's Terms of Service: https://cloud.google.com/maps-platform/terms/.
## Please cite ggmap if you use it! See citation("ggmap") for details.
library(RColorBrewer)
library(gganimate)
con <- dbConnect(RSQLite::SQLite(), "wildfire/FPA_FOD_20170508.sqlite")
numberoffires <- dbGetQuery(con, "SELECT STATE as state, COUNT(DISTINCT FIRE_NAME) AS Count FROM Fires GROUP BY STATE ")
numberoffires$state= abbr2state(numberoffires$state)
numberoffires <- na.omit(numberoffires)
row.names(numberoffires) <- numberoffires$state
numberoffires <- subset(numberoffires, select = -c(state))
choro <- left_join(
map_data("state"),
numberoffires %>%
add_rownames("region") %>%
mutate(region=tolower(region))
)
## Warning: `add_rownames()` was deprecated in dplyr 1.0.0.
## Please use `tibble::rownames_to_column()` instead.
## Joining, by = "region"
ggplot(choro, aes(long, lat)) +
geom_polygon(aes(group = group, fill = Count)) +
coord_quickmap() + labs(title = "Total fires")

surfaceaffected <- dbGetQuery(con, "SELECT f.state as state, SUM(f.Surface) AS Surface FROM (SELECT DISTINCT FIRE_NAME as name, SUM(FIRE_SIZE) as Surface, STATE as state FROM Fires GROUP BY FIRE_NAME) as f GROUP BY f.state")
surfaceaffected$state= abbr2state(surfaceaffected$state)
surfaceaffected <- na.omit(surfaceaffected)
row.names(surfaceaffected) <- surfaceaffected$state
surfaceaffected <- subset(surfaceaffected, select = -c(state))
surface <- left_join(
map_data("state"),
surfaceaffected %>%
add_rownames("region") %>%
mutate(region=tolower(region))
)
## Joining, by = "region"
ggplot(surface, aes(long, lat)) +
geom_polygon(aes(group = group, fill = Surface)) +
coord_quickmap() + labs(title = "Total surface affected")

# 1,88 milion points is too much, one state is enough, my ram can't even handle chrome,
alaskaFires <- dbGetQuery(con, "SELECT F.LATITUDE,F.LONGITUDE,F.FIRE_NAME,F.STATE, F.FIRE_YEAR FROM Fires F WHERE F.STATE = 'AK'")
alaska <- get_stamenmap(bbox = c(left = -180, bottom = 55, right = -120, top = 72), zoom = 5)
## Source : http://tile.stamen.com/terrain/5/0/6.png
## Source : http://tile.stamen.com/terrain/5/1/6.png
## Source : http://tile.stamen.com/terrain/5/2/6.png
## Source : http://tile.stamen.com/terrain/5/3/6.png
## Source : http://tile.stamen.com/terrain/5/4/6.png
## Source : http://tile.stamen.com/terrain/5/5/6.png
## Source : http://tile.stamen.com/terrain/5/0/7.png
## Source : http://tile.stamen.com/terrain/5/1/7.png
## Source : http://tile.stamen.com/terrain/5/2/7.png
## Source : http://tile.stamen.com/terrain/5/3/7.png
## Source : http://tile.stamen.com/terrain/5/4/7.png
## Source : http://tile.stamen.com/terrain/5/5/7.png
## Source : http://tile.stamen.com/terrain/5/0/8.png
## Source : http://tile.stamen.com/terrain/5/1/8.png
## Source : http://tile.stamen.com/terrain/5/2/8.png
## Source : http://tile.stamen.com/terrain/5/3/8.png
## Source : http://tile.stamen.com/terrain/5/4/8.png
## Source : http://tile.stamen.com/terrain/5/5/8.png
## Source : http://tile.stamen.com/terrain/5/0/9.png
## Source : http://tile.stamen.com/terrain/5/1/9.png
## Source : http://tile.stamen.com/terrain/5/2/9.png
## Source : http://tile.stamen.com/terrain/5/3/9.png
## Source : http://tile.stamen.com/terrain/5/4/9.png
## Source : http://tile.stamen.com/terrain/5/5/9.png
## Source : http://tile.stamen.com/terrain/5/0/10.png
## Source : http://tile.stamen.com/terrain/5/1/10.png
## Source : http://tile.stamen.com/terrain/5/2/10.png
## Source : http://tile.stamen.com/terrain/5/3/10.png
## Source : http://tile.stamen.com/terrain/5/4/10.png
## Source : http://tile.stamen.com/terrain/5/5/10.png
ggm <-ggmap(alaska) +
stat_density_2d(
data = alaskaFires,
aes(
x = LONGITUDE,
y = LATITUDE,
fill = stat(level)
),
alpha = .2,
bins = 25,
geom = "polygon"
) +
scale_fill_gradientn(colors = brewer.pal(7, "YlOrRd"))+
labs(title = 'Year: {frame_time}')+
transition_time(FIRE_YEAR)
animate(ggm, fps = 24, duration = 32,renderer=gifski_renderer("alaska.gif"))
## Warning: Removed 4 rows containing non-finite values (stat_density2d).

knitr::include_graphics("alaska.gif")